import * as THREE from 'three';
import { loadGltf } from '../three-utils';

/**
 * 加载地面
 * @returns
 */
export const loadPlane = () => {
  return loadGltf({
    modelPath: '/model/plane/model.glb',
  }).then((gltf) => {
    const group = gltf.scene;
    const mesh = group.children[0].children[0] as THREE.Mesh;
    const material = mesh.material as THREE.MeshPhysicalMaterial;
    // material.color.set('#625b3e');
    material.color.set('#65543f');
    material.side = THREE.BackSide;
    // 检查材质是否有贴图
    if (material.map) {
      // 设置贴图重复
      material.map.repeat.set(20, 20); // 水平和垂直方向上重复4次
      material.map.wrapS = THREE.RepeatWrapping; // 水平方向重复
      material.map.wrapT = THREE.RepeatWrapping; // 垂直方向重复
      material.map.needsUpdate = true; // 更新贴图
    }
    group.children[0].children.splice(1, 1);
    // 因为大地不会离开视锥体，所以关闭裁剪
    group.frustumCulled = false;
    return group;
  });
};
